package com.unicom.office;

import com.unicom.office.impl.*;
import com.unicom.office.runtime.OfficeRuntime;
import com.unicom.office.templatefunc.JoinFunc;
import com.unicom.office.templatefunc.RoundFunc;
import com.unicom.office.templateproc.ExcelFilepathProc;
import com.unicom.office.templateproc.Item2Proc;
import com.unicom.office.var.StringVarItem;

import java.util.*;

public class Test {
    public static void main(String[] args) {
        //testTimeStamp();
        //forJiXu2();
        forTest();
    }

    static void testTimeStamp() {
        Date date = new Date((43886 - 1) * 86400000l);
        date.setYear(date.getYear() - 70);
        System.out.println(date.toString());
    }

    static void forTest(){
        String configForMatch = "test\\configForMatch.xml";
        String templateForMatch = "test\\aaa.csv";
        String dataForMatch = "test\\target.csv";
        String templateForRender = "test\\aaa.xlsx";
        String configForRender = "test\\configForMatch.xml";
        String targetPath = "test\\target.xlsx";

        OfficeRuntime runtime = new OfficeRuntimeImpl();

        // 为提取数据做准备
        OfficeConfigForJSONorXML config = new OfficeConfigForJSONorXML(configForMatch);
        OfficeMatchHelper officeMatchHelper = new OfficeMatchHelper(templateForMatch, config, runtime);
        Map<String, Object> matched = officeMatchHelper.match(dataForMatch);

        // 渲染数据
        config = new OfficeConfigForJSONorXML(configForRender);
        config.register(Item2Proc.INSTANCE);
        //config.register(new RoundFunc()).register(new RowFunc());
        OfficeRenderHelper officeRenderHelper = new OfficeRenderHelper(templateForRender, config, runtime);
        officeRenderHelper.render(matched, targetPath);
    }

    static void forJiXu(){
        String configForMatch = "test\\configForMatch.xml";
        String templateForMatch = "test\\templateForMatch.xlsx";
        String dataForMatch = "test\\dataForMatch.xlsx";

        String templateForRender = "test\\templateForRender.xlsx";
        String configForRender = "test\\configForMatch.xml";
        String targetPath = "test\\target.xlsx";

        OfficeRuntime runtime = new OfficeRuntimeImpl();

        // 为提取数据做准备
        OfficeConfigForJSONorXML config = new OfficeConfigForJSONorXML(configForMatch);
        //config.register(new RowFunc());
        OfficeMatchHelper officeMatchHelper = new OfficeMatchHelper(templateForMatch, config, runtime);
        Map<String, Object> matched = officeMatchHelper.match(dataForMatch);

        // 渲染数据
        config = new OfficeConfigForJSONorXML(configForRender);
        OfficeRenderHelper officeRenderHelper = new OfficeRenderHelper(templateForRender, config, runtime);
        //officeRenderHelper.setBulider(new XlsxEasyBuilder());

        List list = (List) matched.get("list");
        for (Object project : list) {
            matched.put("project", project);
            Map temp = (Map) project;
            String projectName = temp.get("name").toString();
            System.out.println(projectName);
            officeRenderHelper.render(matched, String.format("test\\projects\\%s.xlsx", projectName));
            return;
        }
    }

    static void forJiXu2(){
        String configForMatch = "test\\configForMatch.xml";
        String templateForMatch = "test\\templateForMatch.xlsx";
        String dataForMatch = "test\\dataForMatch.xlsx";
        String templateForRender = "test\\templateForRender.xlsx";
        String configForRender = "test\\configForMatch.xml";
        String targetPath = "test\\target.xlsx";

        OfficeRuntime runtime = new OfficeRuntimeImpl();

        // 为提取数据做准备
        OfficeConfigForJSONorXML config = new OfficeConfigForJSONorXML(configForMatch);
        OfficeMatchHelper officeMatchHelper = new OfficeMatchHelper(templateForMatch, config, runtime);

        Map<String, Object> matched = officeMatchHelper.match(dataForMatch);

        
        // 渲染数据
        config = new OfficeConfigForJSONorXML(configForRender);

        runtime.clear();
        OfficeRenderHelper officeRenderHelper = new OfficeRenderHelper(templateForRender, config, runtime);
        officeRenderHelper.render(matched, targetPath);
    }
}
